package com.stylefeng.guns.modular.code.controller;

import com.stylefeng.guns.common.constant.DSEnum;
import com.stylefeng.guns.config.properties.GunsDemoProperties;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.datasource.DruidProperties;
import com.stylefeng.guns.generator.action.config.WebGeneratorConfig;
import com.stylefeng.guns.generator.action.model.GenQo;
import com.stylefeng.guns.modular.code.factory.DefaultTemplateFactory;
import com.stylefeng.guns.modular.code.service.TableService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成控制器
 *
 * @author fengshuonan
 * @Date 2017年11月30日16:39:19
 */
@Controller
@RequestMapping("/code")
public class CodeController extends BaseController {

    private static String PREFIX = "/code";

    @Autowired
    private TableService tableService;

    @Autowired
    private DruidProperties druidProperties;

    @Autowired
    private GunsDemoProperties gunsDemoProperties;


    /**
     * 跳转到代码生成主页
     */
    @RequestMapping("")
    public String blackboard(Model model) {
        Map<String, List<Map<String, Object>>> dbs = new HashMap<>();
        dbs.put(druidProperties.getDbname(), tableService.getAllTablesInDataSource(druidProperties.getDbname()));
        dbs.put(gunsDemoProperties.getDbname(), tableService.getAllTablesInDataSource(gunsDemoProperties.getDbname()));
        model.addAttribute("dbs", dbs);
        model.addAttribute("defaultDataSourceKey", druidProperties.getDbname());
        model.addAttribute("params", DefaultTemplateFactory.getDefaultParams());
        model.addAttribute("templates", DefaultTemplateFactory.getDefaultTemplates());
        return PREFIX + "/code.html";
    }

    /**
     * 生成代码
     */
    @ApiOperation("生成代码")
    @RequestMapping(value = "/generate", method = RequestMethod.POST)
    @ResponseBody
    public Object generate(GenQo genQo) {
        //设置系统数据源，生成菜单使用
        genQo.setDatasourceKey(DSEnum.DATA_SOURCE_GUNS);
        genQo.setSysDatasource(druidProperties.getDbname());
        genQo.setSysUrl(druidProperties.getUrl());
        genQo.setSysUserName(druidProperties.getUsername());
        genQo.setSysPassword(druidProperties.getPassword());

        if (genQo.getDatasource().equalsIgnoreCase(druidProperties.getDbname())) {
            genQo.setDatasourceKey(DSEnum.DATA_SOURCE_GUNS);
            genQo.setUrl(druidProperties.getUrl());
            genQo.setUserName(druidProperties.getUsername());
            genQo.setPassword(druidProperties.getPassword());
        } else if (genQo.getDatasource().equals(gunsDemoProperties.getDbname())) {
            genQo.setDatasourceKey(DSEnum.DATA_SOURCE_DEMO);
            genQo.setUrl(gunsDemoProperties.getUrl());
            genQo.setUserName(gunsDemoProperties.getUsername());
            genQo.setPassword(gunsDemoProperties.getPassword());
        }
        WebGeneratorConfig webGeneratorConfig = new WebGeneratorConfig(genQo);
        webGeneratorConfig.doMpGeneration();
        webGeneratorConfig.doAdiGeneration();
        return SUCCESS_TIP;
    }
}
